package com.sinosoft.svc.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.log4j.Logger;


public final class ReadFileUtils {

	private static Logger logger = Logger.getLogger(ReadFileUtils.class);
	
	public static String readerMessageOrReportFile(String realPath,String flag){
		String result = null;
		synchronized (RCUtils.class) {
			if (flag != null || flag != "") {
				if (("0").equals(flag)) {
					String filePath0 = null;
					filePath0 = realPath + "logs//"+"receiveMessage.txt";
					File file = new File(filePath0);
					if (file.exists()) {
						result = "["+txtToString(file,filePath0)+"]";
					}
				}
				if (("1").equals(flag)) {
					String filePath1 = null;
					filePath1 = realPath + "logs//"+"receiveReport.txt";
					File file = new File(filePath1);
					if (file.exists()) {
						result = "["+txtToString(file,filePath1)+"]";
					}
				}
			}
			if (flag == null || flag == ""){
				result = "参数为空";
			}
			RCUtils.class.notify();
		}
		return result;
	}
	
	private static String txtToString(File file,String filePath){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close(); 
            OutputStream out = new FileOutputStream(filePath);
            OutputStreamWriter osr = new OutputStreamWriter(out);
            osr.write("");
            osr.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
}